package design_pattern.ProductConsumeMode;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: 蔡翔
 * @Date: 2019/11/15 9:30
 * @Version 1.0
 */
public class ConsumerThread extends Thread {

    public final MessageQueue messageQueue;
    public ConsumerThread(MessageQueue messageQueue,int seq){
        super("CONSUMER "+seq);
        this.messageQueue = messageQueue;
    }
    private static final Random random = new Random(System.currentTimeMillis());

    @Override
    public void run() {
        while (true){
            try {
                Message message = messageQueue.take();
                System.out.println(Thread.currentThread().getName()+" take message "+message.getData());
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
